local desc_hx__skillchanging = [[
  # 神乎其技简介
  ______________________

  ## 注：该模式核心机制神乎其技来自汤圆精品包的阿巴瑟

  ## 身份说明

  支持2~8人游玩，各自为营，活到最后者将作为内奸获得游戏胜利！

  ## 特殊机制
  
  ## 1.神乎其技：发动技能后将失去对应技能，然后若技能数小于20，则获得一个新的技能，且新技能的技能名将必定有原技能名的某个字符（无则随机）；

  ## 2.每次因神乎其技失去技能后会获得1个存在一回合的“神乎其技”（温馨提示：失去的技能的描述会显示在战报中~）；

  ## 3.一名角色的“神乎其技”数为5的倍数/大于5且为2的倍数时其将<font color='red'>失去1点体力并随机弃置1张牌</font>，为10的倍数时其将<font color='red'>减1点体力上限</font>，大于30时则会<font color='red'>被封魔</font>所有技能持续一个阶段；

  ## 4.当一名角色受到伤害后，随机变更其主将（可变成的武将不包括：左慈，界左慈，江山许劭，十常侍，☆SP夏侯惇）；

  ## 5.从第2轮开始时，每轮开始时将随机分配所有角色的座位；

  ## 6.体力值及体力上限默认为4且技能池为房主决定是否包括所有武将技能。
]]


local hx__skillchanging_getLogic = function()
  local hx__skillchanging_logic = GameLogic:subclass("hx__skillchanging_logic")

  function hx__skillchanging_logic:initialize(room)
    GameLogic.initialize(self, room)
    self.role_table = {
      nil,
      { "hidden", "hidden" },
      { "hidden", "hidden", "hidden" },
      { "hidden", "hidden", "hidden", "hidden" },
      { "hidden", "hidden", "hidden", "hidden" , "hidden"},
      { "hidden", "hidden", "hidden", "hidden" , "hidden", "hidden"},
      { "hidden", "hidden", "hidden", "hidden" , "hidden", "hidden", "hidden" , "hidden"},
      { "hidden", "hidden", "hidden", "hidden" , "hidden", "hidden", "hidden" , "hidden", "hidden"},
    }
  end

  function hx__skillchanging_logic:assignRoles()
    local room = self.room
    local n = #room.players
    local roles = self.role_table[n]
    table.shuffle(roles)

    local players = room.players
    for _, p in ipairs(players) do
      room:setPlayerProperty(p, "role", "hidden")
      room:broadcastProperty(p, "role")
      room:setPlayerProperty(p, "role_shown", true)
    end
    room:setBanner("@[:]huixiangmode_rule","hx__skillchanging-intro") --模式简介
    --决定是否启用全扩技能池
    local function getPlayerByScreenName(Room, name)
      return table.find(Room.players, function(pl)
        return pl._splayer:getScreenName() == name
      end)
    end
    local croom = room.room
    local cowner = croom:getOwner()
    local owner = getPlayerByScreenName(room, cowner:getScreenName())
    room:doBroadcastNotify("ShowToast", "请等待房主决定是否启用全扩技能池")
    room:sendLog{ type = "请等待房主决定是否启用全扩技能池", toast = true }
    local choice = room:askToChoice(owner, {
      choices = {"yes","no"},
      skill_name = "game_rule",
      cancelable = false,
      prompt = "是否将神乎其技的技能池改为全扩？",
    })
    if choice == "yes" then
      room:setTag("hx__skillchanging_all", true)
      room:doBroadcastNotify("ShowToast", "房主已启用全扩技能池！！")
      room:sendLog{ type = "房主已启用全扩技能池！！", }
    else
      room:setTag("hx__skillchanging_all", false)
      room:doBroadcastNotify("ShowToast", "本局全扩技能池未启用")
      room:sendLog{ type = "本局全扩技能池未启用", }
    end
  end

  function hx__skillchanging_logic:adjustSeats()
    local player_circle = {}
    local room = self.room
    local players = self.room.players
    local p = 1
    local x = math.random(1,#players)
    local one = players[x]  --随机的1号位
    for i = 1, #players do
      if players[i] == one then
        p = i
        break
      end
    end
    for j = p, #players do
      table.insert(player_circle, players[j])
      local num = j-p +1
      room:doBroadcastNotify("ShowToast", "温馨提示：你是" .. num .. "号位", {players[j]})
      room:doBroadcastNotify("ShowToast", "温馨提示：你是" .. num .. "号位", {players[j]})
      room:doBroadcastNotify("ShowToast", "温馨提示：你是" .. num .. "号位", {players[j]})
    end
    for j = 1, p - 1 do
      table.insert(player_circle, players[j])
      local num = j +3
      room:doBroadcastNotify("ShowToast", "温馨提示：你是" .. num .. "号位", {players[j]})
      room:doBroadcastNotify("ShowToast", "温馨提示：你是" .. num .. "号位", {players[j]})
      room:doBroadcastNotify("ShowToast", "温馨提示：你是" .. num .. "号位", {players[j]})
    end

    self.room:arrangeSeats(player_circle)
  end

  function hx__skillchanging_logic:chooseGenerals()
    local room = self.room ---@type Room
    local generalNum = room.settings.generalNum
    local n = room.settings.enableDeputy and 2 or 1
    for _, p in ipairs(room.players) do
      room:setPlayerProperty(p, "role_shown", true)
      room:broadcastProperty(p, "role")
    end

    local players = room.players
    local lord = players[1]
    room:setCurrent(lord)
    local generals = table.random(room.general_pile, #room.players * generalNum)

    local req = Request:new(players, "AskForGeneral")
    for i, p in ipairs(players) do
      local arg = table.slice(generals, (i - 1) * generalNum + 1, i * generalNum + 1)
      req:setData(p, { arg, n })
      req:setDefaultReply(p, table.random(arg, n))
    end

    for _, p in ipairs(players) do
      local result = req:getResult(p)
      local general, deputy = result[1], result[2]
      room:prepareGeneral(p, general, deputy)
    end

    -- room:sendLog{ type = "7月9日更新：①第2轮起每轮开始时将随机分配座位", toast = true }
    -- room:sendLog{ type = "7月9日更新：②战报将显示失去的技能的技能描述", toast = true }
    -- room:sendLog{ type = "7月9日更新：③神乎其技数大于5且为2的倍数时也可触发5倍的负面", toast = true }

    room:askToChooseKingdom(players)
    room:setTag("SkipNormalDeathProcess", true)

  end

  function hx__skillchanging_logic:attachSkillToPlayers()
    local room = self.room
    local players = room.players
    local n = room.settings.enableDeputy and 2 or 1
    local addRoleModSkills = function(player, skillName)
      local skill = Fk.skills[skillName]
      if not skill then
        fk.qCritical("Skill: "..skillName.." doesn't exist!")
        return
      end
      if skill:hasTag(Skill.Lord) then return end

      -- if skill:hasTag(Skill.AttachedKingdom) and not
      -- table.contains(skill:getSkeleton().attached_kingdom, player.kingdom) then return end

      room:handleAddLoseSkills(player, skillName, nil, false,true)
      self:trigger(fk.EventAcquireSkill, player, {skill = skill, who = player})
    end
    for _, p in ipairs(room.alive_players) do
      for _, s in ipairs(Fk.generals[p.general]:getSkillNameList(false)) do
        addRoleModSkills(p, s)
      end
      local deputy = Fk.generals[p.deputyGeneral]
      if deputy then
        for _, s in ipairs(deputy:getSkillNameList(true)) do
          addRoleModSkills(p, s)
        end
      end
    end
  end
  return hx__skillchanging_logic
end

local hx__skillchanging_mode = fk.CreateGameMode{
  name = "hx__skillchanging_mode",
  minPlayer = 2,
  maxPlayer = 8,
  rule = "#hx__skillchanging_rule&",
  logic = hx__skillchanging_getLogic,
  is_counted = function(self, room)
    return #room.players >= 1
  end,
  surrender_func = function(self, playedTime)
    return { {
      text = "hx__skillchanging: left two alive",
      passed = #table.filter(Fk:currentRoom().players, function(p)
        return p.rest > 0 or not p.dead
      end) == 2
    } }
  end,
  winner_getter = function(self, victim)
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end
    local room = victim.room
    local alive = table.filter(room.players, function(p)
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)
    if #alive > 1 then
      if #room.players >= 5 then
        victim.role = "renegade"
      end
      return ""
    end
    local pname = alive[1]._splayer:getScreenName()
    --[[😩--]]
    alive[1].role = "renegade"
    room:broadcastProperty(alive[1], "role")
    return "renegade"
  end,
  get_adjusted = function (self, player)
    if player.role then
      return {hp = 4, maxHp = 4}
    end
    return {}
  end
}

Fk:loadTranslationTable{
  ["hx__skillchanging_mode"] = "神乎其技",
  [":hx__skillchanging_mode"] = desc_hx__skillchanging,
  ["#hx__skillchanging_rule&"] = "神乎其技规则",
  ["hx__skillchanging: left two alive"] = "仅剩两名角色存活",
  ["hx__skillchanging-intro"] = "神乎其技",
  [":hx__skillchanging-intro"] = [[
  <br/>特殊机制：
  <br/>1.神乎其技：发动技能后将失去对应技能，然后若技能数小于20，则获得一个新的技能，且新技能的技能名将必定有原技能名的某个字符（无则随机）；
  <br/>2.每次因神乎其技失去技能后会获得1个存在一回合的“神乎其技”（温馨提示：失去的技能的描述会显示在战报中~）；
  <br/>3.一名角色的“神乎其技”数为<font color='red'>5的倍数/大于5且为2的倍数时其将失去1点体力并随机弃置1张牌</font>，为<font color='red'>10的倍数时其将减1点体力上限</font>，大于30时则会被封魔所有技能持续一个阶段；
  <br/>4.当一名角色受到伤害后，随机变更其主将（可变成的武将不包括：左慈，界左慈，江山许劭，十常侍，☆SP夏侯惇）；
  <br/>5.从第2轮开始时，每轮开始时将随机分配所有角色的座位；
  <br/>6.体力值及体力上限默认为4且技能池为房主决定是否包括所有武将技能。<br/><br/>
  <br/> <b>祝你活到最后！</b>]],
}

return hx__skillchanging_mode
